<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Sms\SmsReplyRepository;
use App\Repository\Sms\SmsSendLogRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

// 分销商
class MessageReplyController extends BaseController
{
    public function __construct(SmsReplyRepository $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('number'))) {
            $where['where'] = ['phone' => $request->get('number')];
        }
        if ($request->has('is_reply') && ($request->get('is_reply') != '')) {
            $where['where'] = ['is_reply' => $request->get('is_reply')];
        }
        if ($request->has('text') && ($request->get('text') != '')) {
            $where['where'] = ['content' => $request->get('text')];
        }
        if (($request->has('params'))) {
            // var_dump($request->get('params'), 111111);
            $times = $request->get('params');
            $where['between'] = ['created_at' => [date('Y-m-d 00:00:00', strtotime($times['beginTime'])), date('Y-m-d 23:59:59', strtotime($times['endTime']))]];
        }
        if (!empty($request->get('pageNum'))) {
            $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
            $where['limit']['number'] = $request->get('pageSize');
        }

        if ($request->user->role_id != 1 && $request->user->shop_id != 5) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        $where['select'] = ['*'];
        $data = $this->repoist->index($where);
        return $data;
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id')];
        return $this->repoist->edit($where, $request);
    }

    public function show(Request $request, $id)
    {
        $where = ['phone' => $id];
        //查询一条数据  查询的是回复的数据
        $return = $this->repoist->lists($where, 1, 9999999);
        if ($return) {
            $datas = get_url_content1('http://www.yuxuantongxin.com:32003/api/v1/getExtOrderList?phone=' . $id);

            $dataA = json_decode($datas, true);
            if (!empty($dataA['data'])) {
                $return['orders'] = !empty($dataA['data']) ? $dataA['data'] : '';
            }
            if (empty($return['orders']['order']['data'])) {
                $where = [
                    'pay_phone' => $id
                ];
                $datasA = DB::table('order')->where($where)->orWhere(['iccid' => $id])->get();
                // var_dump($datasA);
                if ($datasA) {
                    $data = [];
                    foreach ($datasA as $key => $val) {
                        $data[] = [
                            'pagname' => $val->goods_name,
                            'orderno' => $val->orderno,
                            'phone' => $val->pay_phone,
                            'mobile' => $val->pay_phone,
                            'statusDesc' => $val->status_desc,
                            'created_at' => $val->created_at,
                        ];
                        $orderId = $val->id;
                    }
                }
                $return['orders']['order']['data'] = $data;
            }

            if (empty($return['orders']['sms']['data'])) {
                $model = new SmsSendLogRepository();
                $where['where'] = ['phone' => $id];
                $newDatas = $model->index($where);
                $smsData = [];
                if (!empty($newDatas['data'])) {
                    foreach ($newDatas['data'] as $key => $val) {
                        $smsData[] = [
                            'tmp_title' => $val['smsTmpName'],
                            'content' => $val['content'],
                            'created_at' => $val['createdAt'],
                        ];
                    }
                }
                if (empty($smsData) && !empty($orderId)) {
                    $where['where'] = ['order_id' => $orderId];
                    $newDatas = $model->index($where);
                    if (!empty($newDatas['data'])) {
                        foreach ($newDatas['data'] as $key => $val) {
                            $smsData[] = [
                                'tmp_title' => $val['smsTmpName'],
                                'content' => $val['content'],
                                'created_at' => $val['createdAt'],
                            ];
                        }
                    }
                }
                $return['orders']['sms']['data'] = $smsData;
            }
        }
        return $return;
        // 查询订单的 订单信息
        // $res

        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }
        $where['where']['shop_id'] = $request->user->shop_id;
        // var_dump($where);
        $return = $this->repoist->lists($where, 1, 99999);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->del($where);
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        $update = [
            'status' => $request->get('status'),
        ];
        return $this->repoist->edit($where, $request);
    }
}
